package day20.zhiyou100;

// concurrent 并发
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

// 线程池执行器
public class ExecutorsPool {

    public static void fixed() {
        // 创建了一个含有3个固定线程的线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
        // 执行submit()方法
        for (int i = 0; i < 10; i++) {
            Future<?> future = threadPool.submit(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 5; i++) {
                        System.out.println(Thread.currentThread().getName() + "----->" + i);
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            System.out.println("当前执行的线程次数：" + i);
        }
        threadPool.shutdown();
    }

    // 可变
    public static void change() {
        // 可变线程池
        ExecutorService threadPool2 = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            threadPool2.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "线程在打印---->");
                }
            });
           /* threadPool2.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"线程在打印---->");
                }
            });*/
        }
        // 关闭线程池
        threadPool2.shutdown();
    }

    public static void main(String[] args) {
        change();
    }
}
